﻿#if UNITY_EDITOR
using System;
using Cinemachine;
using CriWare;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
// using UnityEditor.Animations;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    [ExecuteAlways]
    public class SkillEditorMainUpdate : EditorSceneUpdate
    {
        public static SkillEditorMainUpdate Instance => instance as SkillEditorMainUpdate;

        public CinemachineVirtualCamera vc;
        public CriAtomSource criSource;
        public CriAtom atom;
        public CinemachineVirtualCamera charaVc;
        public Transform roleVcFollowRoot;
        public CriManaMovieControllerForUI videoPlayer;
        public CanvasGroup moviePanel;
        
        [HideInInspector] public UnitView unit;
        [HideInInspector] public UnitView target;
        [HideInInspector] public bool showCameraCurve;
        
        protected override void OnInitialize()
        {
            this.atom.Setup();
            this.criSource.CallAwake();
            
            if (CriManaPlugin.IsLibraryInitialized() == false)
            {
                CriWareInitializer cfg = GameObject.FindObjectOfType<CriWareInitializer>();
                if (cfg != null)
                {
                    CriWareInitializer.InitializeMana(cfg.manaConfig);
                }
                else
                {
                    CriWareInitializer.InitializeMana(new CriManaConfig());
                    Debug.Log("[CRIWARE] Timeline / Mana: Can't find CriWareInitializer component; Using default parameters in edit mode.");
                }
            }
            // this.videoPlayer.CallAwake();
        }

        public void PlayAudioById(string cueName, string cueSheetName, string selector, string label)
        {
            AudioSourceType sourceType = AudioSourceType.SfxSource;
            GetOrAddCueSheet(cueSheetName, sourceType);
            this.criSource.cueSheet = cueSheetName;
            this.criSource.cueName = cueName;
            var playback = this.criSource.Play();
        }
        
        private CriAtomCueSheet GetOrAddCueSheet(string cueSheetName, AudioSourceType sourceType)
        {
            var sheet = CriAtom.GetCueSheet(cueSheetName);

            if (sheet == null)
            {
                var acbPath = PathConstant.GetAudioAcbFilePath(cueSheetName, sourceType, out _);
                var awbPath = PathConstant.GetAudioAwbFilePath(cueSheetName, sourceType, out var awbInPersistent);

                sheet = CriAtom.AddCueSheet(cueSheetName, acbPath, awbFile: awbPath);
            }
            return sheet;
        }
    }
}
#endif